﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using miniBoxClassLibrary;
using System.Collections.Specialized;
using System.IO;

namespace Upload_WebSvc
{
    /// <summary>
    /// FileUpload 的摘要描述
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務，請取消註解下一行。
    // [System.Web.Script.Services.ScriptService]
    public class FileUpload : System.Web.Services.WebService
    {
        private APUtil.clsDataSrc oDataSrc;
        private miniBoxClassLibrary.clsDataSrc_cshap oDataSrc1;
        private APUtil.clsLog oLog = new APUtil.clsLog();

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }



        [WebMethod(Description = "编辑箱内物品(含档案上传)")]
        public string UploadFile()
        {
            MemberClass mc = new MemberClass();
            bool bResult = false;
            String sRetjson = string.Empty;
            String sJson_Tmp = string.Empty;
            String Client = HttpContext.Current.Request.UserHostAddress;
            String sHomeFolder = System.Configuration.ConfigurationSettings.AppSettings["HomeFolder"];
            String fromIP = HttpContext.Current.Request.UserHostAddress;
            try
            {
                oDataSrc1 = new clsDataSrc_cshap();
                
                oLog.writeLine("编辑箱内物品(含档案上传)", "", Client, "UploadFile", APUtil.clsLog.enumMsgType.General, "===================START================" );
                oDataSrc1.BeginTrans();
                HttpFileCollection postCollection = Context.Request.Files;
                HttpPostedFile aFile = postCollection["ITEM_IMAGE"];
                String sBOX_NO = string.Empty;
                String sITEM_ID = string.Empty;
                String sITEM_TYPE = string.Empty;
                String sITEM_DESC = string.Empty;
                NameValueCollection FormVars = Context.Request.Form;
                String file_Path = string.Empty;
                String file_url = string.Empty;
                String newFileName = string.Empty;
                String sId = string.Empty;
                String sLogintype = string.Empty;
                String ssToken = string.Empty;
               
                sId = FormVars["ID"];
                sLogintype = FormVars["LOGINTYPE"];
                ssToken = FormVars["sToken"];
                bResult = oDataSrc1.Check_Token(sId, sLogintype, ssToken);
                if (bResult == false)
                {
                    mc.sResult_Value = "-2";
                    mc.sMsg_Value = "TOKEN验证失败";
                    sJson_Tmp = " {";
                    sJson_Tmp = sJson_Tmp + @"""Status"" : """ + mc.sResult_Value + @""",";
                    sJson_Tmp = sJson_Tmp + @"""Msg"" : """ + mc.sMsg_Value + @""" ";
                    sJson_Tmp = sJson_Tmp + "}";
                    oLog.writeLine("编辑箱内物品(含档案上传)", "", Client, "UploadFile", APUtil.clsLog.enumMsgType.General, "TOKEN验证失败:" + sJson_Tmp);
                    return sJson_Tmp;
                }

                sBOX_NO = FormVars["BOX_NO"];
                sITEM_ID = FormVars["ITEM_ID"];
                sITEM_TYPE = FormVars["ITEM_TYPE"];
                sITEM_DESC = FormVars["ITEM_DESC"];
                newFileName = sBOX_NO + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(aFile.FileName);
                oLog.writeLine("编辑箱内物品(含档案上传)", "", Client, "UploadFile", APUtil.clsLog.enumMsgType.General, "ITEM_TYPE:" + sITEM_TYPE + ",ITEM_DESC:" + sITEM_DESC);
                try
                {
                    oLog.writeLine("编辑箱内物品(含档案上传)", "", Client, "UploadFile", APUtil.clsLog.enumMsgType.General, "ITEM_IMAGE:" + aFile.FileName);
                }
                catch (Exception ex)
                {
                    mc.sResult_Value = "-1";
                    mc.sMsg_Value = ex.Message.Replace(@"""", " ");
                    //write log
                    oLog.writeLine("编辑箱内物品(含档案上传)", "", Client, "UploadFile", APUtil.clsLog.enumMsgType.General, mc.sMsg_Value);
                    oDataSrc1.RollBack();
                }
                file_Path = sHomeFolder.Trim('\'') + sBOX_NO + @"\";
                if (Directory.Exists(file_Path) == false) { Directory.CreateDirectory(file_Path); }
                //aFile.SaveAs(file_Path + Path.GetFileName(aFile.FileName));

                aFile.SaveAs(file_Path + newFileName);
                file_url = System.Configuration.ConfigurationSettings.AppSettings["FileBaseURL"].TrimEnd('\'')
                    + sBOX_NO + @"/" + newFileName;
                mc.sResult_Value = oDataSrc1.Clear_BoxcontentByItem(sITEM_ID);
                if (mc.sResult_Value != "0")
                {
                    oLog.writeLine("编辑箱内物品(含档案上传)", "", Client, "UploadFile", APUtil.clsLog.enumMsgType.General, mc.sMsg_Value);
                    throw new Exception(mc.sResult_Value);
                }
                mc.sResult_Value = oDataSrc1.Add_Boxcontent(sBOX_NO, sITEM_TYPE, sITEM_DESC, file_url);
                if (mc.sResult_Value != "0")
                {
                    oLog.writeLine("编辑箱内物品(含档案上传)", "", Client, "UploadFile", APUtil.clsLog.enumMsgType.General, mc.sMsg_Value);
                    throw new Exception(mc.sResult_Value);
                }
                else 
                {
                    oDataSrc1.Commit();
                    oLog.writeLine("编辑箱内物品(含档案上传)", "", Client, "UploadFile", APUtil.clsLog.enumMsgType.General, "===================END================");
                }
            }
            catch (Exception ex)
            {
                mc.sResult_Value = "-1";
                mc.sMsg_Value = ex.Message.Replace(@"""", " ");
                //write log
                oLog.writeLine("编辑箱内物品(含档案上传)", "", Client, "UploadFile", APUtil.clsLog.enumMsgType.General, mc.sMsg_Value);
                oDataSrc1.RollBack();
            }
            finally { oDataSrc1 = null; }
            sJson_Tmp = " {";
            sJson_Tmp = sJson_Tmp + @"""Status"" : """ + mc.sResult_Value + @""",";
            sJson_Tmp = sJson_Tmp + @"""Msg"" : """ + mc.sMsg_Value + @""" ";
            sJson_Tmp = sJson_Tmp + "}";
            return sJson_Tmp;
        }
    }
}
